package cn.iocoder.yudao.module.ent.service.statistics;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ent.controller.admin.statistics.vo.GroupStatisticsRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.statistics.vo.IndexReqVO;
import cn.iocoder.yudao.module.ent.dal.dataobject.followrecord.FollowRecordDO;
import cn.iocoder.yudao.module.ent.dal.mysql.followrecord.FollowRecordMapper;
import cn.iocoder.yudao.module.ent.dal.mysql.statistics.StatisticsMapper;
import cn.iocoder.yudao.module.ent.enums.EntFollowType;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Service
@Validated
public class StatisticsServiceImpl implements StatisticsService {


    @Resource
    private StatisticsMapper statisticsMapper;

    @Resource
    private FollowRecordMapper followRecordMapper;

    @Resource
    private DeptApi deptApi;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public List<GroupStatisticsRespVO> groupStatisticsByUserId(Long userId) {
        return statisticsMapper.groupStatistics(userId);
    }

    @Override
    public Long getReceiveEntCount(IndexReqVO indexReqVO) {
        if (CollUtil.isEmpty(indexReqVO.getUserIds())) {
            return 0L;
        }
        return followRecordMapper.selectCount(
                new LambdaQueryWrapperX<FollowRecordDO>()
                        .eqIfPresent(FollowRecordDO::getType, EntFollowType.RECEIVE.getType())
                        .in(FollowRecordDO::getUserId, indexReqVO.getUserIds())
                        .betweenIfPresent(FollowRecordDO::getCreateTime, indexReqVO.getTime()));
    }

    @Override
    public Long getFollowedEntCount(IndexReqVO indexReqVO) {
        if (CollUtil.isEmpty(indexReqVO.getUserIds())) {
            return 0L;
        }
        return followRecordMapper.selectCount(
                new LambdaQueryWrapperX<FollowRecordDO>()
                        .in(FollowRecordDO::getType, ListUtil.toList(EntFollowType.REMARK.getType(), EntFollowType.TAG.getType(), EntFollowType.CALL.getType()))
                        .in(FollowRecordDO::getUserId, indexReqVO.getUserIds())
                        .betweenIfPresent(FollowRecordDO::getCreateTime, indexReqVO.getTime()));
    }

    @Override
    public List<Long> getUserIds(IndexReqVO reqVO) {

        // 情况一：选中某个用户
        if (ObjUtil.isNotNull(reqVO.getUserId())) {
            return ListUtil.of(reqVO.getUserId());
        }
        // 情况二：选中某个部门
        // 2.1 获得部门列表
        List<Long> deptIds = convertList(deptApi.getChildDeptList(reqVO.getDeptId()), DeptRespDTO::getId);
        deptIds.add(reqVO.getDeptId());
        // 2.2 获得用户编号
        return convertList(adminUserApi.getUserListByDeptIds(deptIds), AdminUserRespDTO::getId);
    }
}
